package com.letu.core.utils;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.CellStyle;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 基于Hutool excel 工具类.
 *
 * @author dqq
 * @date 2023 /3/10
 */
public class ExcelUtils {

    /**
     * excel 导出.
     *
     * @param rows the rows
     * @param out  the out
     */
    public static void export(List<Map<String, Object>> rows, OutputStream out) {
        ExcelWriter writer = ExcelUtil.getWriter(true);
        CellStyle headCellStyle = writer.getHeadCellStyle();
        headCellStyle.setWrapText(true);
        CellStyle cellStyle = writer.getCellStyle();
        cellStyle.setWrapText(true);

        export(writer, rows, out);
    }

    /**
     * excel 导出.
     *
     * @param writer the writer
     * @param rows   the rows
     * @param out    the out
     */
    public static void export(ExcelWriter writer, List<Map<String, Object>> rows, OutputStream out) {
        writer.write(rows, true);
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }
}